package com.yellowpage.ca.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.yellowpage.ca.data.UserUsergroup;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.BaseDaoHibernateImpl;

public class UserUsergroupDaoImpl extends BaseDaoHibernateImpl<UserUsergroup, Long>
		implements UserUsergroupDao {

	public UserUsergroupDaoImpl() {
		super(UserUsergroup.class);
	}
	
	public List removeByUsergroup(Long userGroupId){
		String queryString = "select ug.userUsergroupId from UserUsergroup ug where ug.userGroupId = "
				+ userGroupId;
		return getHibernateTemplate().find(queryString);
	}
	

	/**
	 * 关联用户
	 */
	public boolean bindUser(final String[] userIds, final Long userGroupId) {
//		this.getHibernateTemplate().execute(new HibernateCallback() {
//			public Object doInHibernate(Session session)
//					throws HibernateException {
//
//				if (userIds != null) {
//					for (String userId : userIds) {
//						if (userId != null && !userId.equals("")) {
//							String delSql = "delete from ca_user_usergroup t where t.user_id ="
//									+ userId
//									+ " and t.user_group_id = "
//									+ userGroupId;
//							session.createSQLQuery(delSql).executeUpdate();
//							String insertSql = "insert into ca_user_usergroup(user_id,user_group_id) "
//									+ "values ("
//									+ userId
//									+ ","
//									+ userGroupId
//									+ ")";
//							session.createSQLQuery(insertSql).executeUpdate();
//						}
//					}
//				}
//				return true;
//			}
//		}, true);

		return true;
	}

	/**
	 * 查找已经被关联的用户
	 */
	public List searchBindedUser(final Long userGroupId , final String name , final PageRender pageBean) {
//		String queryString = "select u from User u inner join u.userGroups g where u.name like '" + name +"' and g.userGroupId = "
//				+ userGroupId.toString();
//		return getHibernateTemplate().find(queryString);
		
		
		Session session= this.getSessionFactory().openSession();
		
		String queryString = "select u from User u inner join u.userGroups g where u.name like '"
				+ name + "' and g.userGroupId = " + userGroupId.toString();

		Query query = session.createQuery(queryString);
		if (pageBean != null) {
			query.setFirstResult(pageBean.getStartNum());
			query.setMaxResults(pageBean.getEndNum() - pageBean.getStartNum());
		}
		return query.list();
			
	}

	/**
	 * 删除关联
	 */
	public boolean unbindUser(final String[] userIds, final Long userGroupId) {
//		this.getHibernateTemplate().execute(new HibernateCallback() {
//			public Object doInHibernate(Session session)
//					throws HibernateException {
//
//				if (userIds != null) {
//					for (String userId : userIds) {
//						if (userId != null && !userId.equals("")) {
//							String delSql = "delete from ca_user_usergroup t where t.user_id ="
//									+ userId
//									+ " and t.user_group_id = "
//									+ userGroupId;
//							session.createSQLQuery(delSql).executeUpdate();
//						}
//					}
//				}
//				return true;
//			}
//		}, true);
		
		return true;
	}

}
